package com.deep.bilibili.service;

import com.deep.bilibili.dao.FileMapper;
import com.deep.bilibili.entity.File;
import com.deep.bilibili.util.LiveUtil;
import com.deep.bilibili.vo.UploadFileVo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class FileServiceImpl implements FileService {
    private static Logger logger = LoggerFactory.getLogger(FileService.class);


    @Autowired
    FileMapper fileMapper;

    @Override
    public File save(UploadFileVo uploadFileVo) {

        // 增加附件管理
        File file1 = new File();
        file1.setAddTime(new Date());
        file1.setUpdateTime(new Date());
        file1.setIsDel(0);
        file1.setType(1);
        file1.setName(uploadFileVo.getFileName());
        file1.setPath(uploadFileVo.getFilePath());
        file1.setUrl(uploadFileVo.getFileUri());
//        file1.setUrl("https://i0.hdslb.com/bfs/feed-admin/a59a0927ca5912f66223d4a715787f6f6f414f2f.jpg@976w_550h_1c.webp");
        file1.setMime(uploadFileVo.getMimeType());
        file1.setSize(uploadFileVo.getSize());
        file1.setCover(uploadFileVo.getCover());
        file1.setCoverPath(uploadFileVo.getCoverPath());

        String streamName = uploadFileVo.getStreamName();
        file1.setStreamName(streamName);
        file1.setRtmpUrl(LiveUtil.getRtmpUrl(streamName));
        file1.setFlvUrl(LiveUtil.getFlvUrl(streamName));
        file1.setHlsUrl(LiveUtil.getHlsUrl(streamName));
        file1.setWebrtcUrl(LiveUtil.getWebrtcUrl(streamName));

        int fileId = fileMapper.insert(file1);
        logger.info("file1 =  {}", file1);

//        return fileId;
        return file1;
    }
}
